<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;

class Recruit extends Model
{
    //
    protected $table = 'recruit';
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo('App\Models\Home\Users','uid','id');
    }

    public function assign_user()
    {
        return $this->belongsTo('App\Models\Home\Users','assign','id');
    }

    public function getList($skip,$limit,$opt=array(),$user=true,$assign=true){

        if (!isset($limit)||!isset($skip)){
            return [];
        }
        if (!isset($skip)){
            $skip = 0;
        }

        $query = Recruit::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        $query = $query->skip($skip)->limit($limit);

        if ($user){
            $query = $query->with(['user']);
        }

        if ($assign){
            $query = $query->with(['assign_user']);
        }

        $list = $query->get();

        return $list;
    }

    public function getCount($opt=array()){

        $query = Recruit::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        return $query->count();
    }


}
